package com.carlosk.kandicar.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.carlosk.kandicar.R;
import com.carlosk.kandicar.ui.base.BaseApp;

/**
 * 本地存储的工具类
 * 
 * @author carlos carlosk@163.com
 * @version 创建时间：2012-12-11 下午3:16:42
 */

public class ReferenceUtils extends BaseUtils {
	public static final String TAG = "ReferenceUtils";

	/**
	 * 根据key返回value，value必须为字符串
	 * 
	 * @param key
	 * @return
	 */
	public static String getStringByKey(String key) {
		SharedPreferences sharedPreferences = BaseApp.AppContext
				.getSharedPreferences(ValueUtil.getString(R.string.app_name),
						Context.MODE_PRIVATE);
		String cString = sharedPreferences.getString(key, "");
		if (ValueUtil.isStrEmpty(cString)) {
			cString = "";
		}
		return cString;
	}

	public static int getIntByKey(String key,int defaultValue) {
		SharedPreferences sharedPreferences = BaseApp.AppContext
				.getSharedPreferences(ValueUtil.getString(R.string.app_name),
						Context.MODE_PRIVATE);
		return sharedPreferences.getInt(key, defaultValue);
	}

	/**
	 * 保存key和value
	 * 
	 * @param key
	 * @param content
	 */
	public static void setString(String key, String content) {
		SharedPreferences sharedPreferences = BaseApp.AppContext
				.getSharedPreferences(ValueUtil.getString(R.string.app_name),
						Context.MODE_PRIVATE);
		Editor edit = sharedPreferences.edit();
		edit.putString(key, content);
		edit.commit();
	}

	public static void setInt(String key, int content) {
		SharedPreferences sharedPreferences = BaseApp.AppContext
				.getSharedPreferences(ValueUtil.getString(R.string.app_name),
						Context.MODE_PRIVATE);
		Editor edit = sharedPreferences.edit();
		edit.putInt(key, content);
		edit.commit();
	}
}
